/* Filename: flen.c
 * Description: 获取文件大小
 *   gcc -Wall -D_FILE_OFFSET_BITS=64 -o flen flen.c
 * Last modified: humble 20200301 19:18
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

static void usage(void);

static off_t flen(const char *pathname) {
    struct stat statres;
    if(stat(pathname, &statres) < 0){ fprintf(stderr, "stat failed\n"); return -1; }
    return statres.st_size; // off_t类型是不知道的，所以需要在编译的时候指定一下-D_FILE_OFFSET_BITS=64
}

int main(int argc, char **argv) {
    if(argc != 2){ fprintf(stderr, "argc !=2\n"); usage(); exit(1); }
    printf("%lld\n", (long long)flen(argv[1])); //64位整型，用longlong
    return 0;
}

static void usage(void) {
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
